package cn.com.dhcc.app.pub.module.login.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.dhcc.app.core.annotation.SiNoNeedLogin;
import cn.com.dhcc.app.core.exception.NiceException;
import cn.com.dhcc.app.core.handler.ssohandler.SsoUser;
import cn.com.dhcc.app.core.handler.ssohandler.UserSessionVo;
import cn.com.dhcc.app.core.pubmodule.login.controller.LoginCallback;
import cn.com.dhcc.app.core.pubmodule.login.controller.LoginCommon;
import cn.com.dhcc.app.core.pubmodule.sysmgr.authmgr.vo.RoleInfo;
import cn.com.dhcc.app.core.util.CollectionUtil;

/**
 * 包含非中央节点用户登录，注销，注册等功能，
 * 
 * @see {@link cn.com.dhcc.app.pub.core.interceptor.core.interceptor.AuthInterceptor} 权限验证
 * @日期：2012-12-27下午4:54:28
 * @作者：乔兵
 * @版权所有：东华软件
 * @版本：1.0
 */
@Controller
@RequestMapping("pub/")
public class LoginController extends cn.com.dhcc.app.core.base.BaseController {


	@Autowired
	LoginCommon loginCommon;
	/**
	 * 登录验证 针对没有单点登录的节点
	 * @param model
	 * @param httpSession
	 * @param userCode
	 * @param password
	 * @return
	 */
	@RequestMapping(LoginCommon.LOGIN_REQUESTMAPPING)
	@SiNoNeedLogin
	public String login(final Model model,HttpServletRequest request, HttpSession httpSession, String userName,
			String password) {
		return loginCommon.login(request, model, httpSession, userName, password,new LoginCallback() {
			@Override
			public SsoUser getSsoUserByUserName(String userName, String password)
					throws NiceException {
				return null;
			}
			@Override
			public void customLogin(UserSessionVo sessionVo) throws NiceException {
			}
		});
	}
	@SiNoNeedLogin
	@RequestMapping(LoginCommon.LOGOUT_REQUESTMAPPING)
	public String logout(HttpSession httpSession) {
		return loginCommon.logout(httpSession);
	}
	/**
	 * 将接口的角色对象转换成框架内部的角色对象
	 */
    @SuppressWarnings("unused")
    private static List<RoleInfo> changeToAppRoinfoList(List<?> roleInfos ){
    	List<RoleInfo> appRoleInfos = null;
    	if(CollectionUtil.hasElement(roleInfos)){
    		appRoleInfos = new ArrayList<RoleInfo>();
    		for (Iterator<?> iterator = roleInfos.iterator(); iterator
					.hasNext();) {
				RoleInfo appRoleInfo = new RoleInfo();
			}
    	}
    	return appRoleInfos;
    }
}
